package com.pinyoumei.app.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.pinyoumei.app.R;
import com.pinyoumei.app.activity.ProductDetailActivity;
import com.pinyoumei.app.activity.ProductDetailPtActivity;
import com.pinyoumei.app.adapter.ShoppingCartAdapter;
import com.pinyoumei.app.bean.GoodDetailsBean;
import com.pinyoumei.app.bean.ShopCartListBean;
import com.pinyoumei.app.network.Constants;
import com.pinyoumei.app.utils.SPUtils;
import com.pinyoumei.app.utils.SignatureUtils;
import com.pinyoumei.app.utils.Utils;
import com.pinyoumei.app.view.LoadingDailog;

import java.util.HashMap;
import java.util.Map;

import me.leefeng.lfrecyclerview.LFRecyclerView;
import me.leefeng.lfrecyclerview.OnItemClickListener;

@SuppressLint("ValidFragment")
public class ShoppingCartFragment  extends BaseLazyLoadFragment implements View.OnClickListener, OnItemClickListener, LFRecyclerView.LFRecyclerViewListener {

    private String mTitle;
    private LFRecyclerView recycleview;
    private ShoppingCartAdapter shoppingCartAdapter;
    private LoadingDailog loadingDailog;
    private Gson gson;

    public static ShoppingCartFragment getInstance(String title) {
        ShoppingCartFragment sf = new ShoppingCartFragment();
        sf.mTitle = title;
        return sf;
    }

    @Override
    public int getLayoutId() {
        return R.layout.shopping_cart_view;
    }

    @Override
    public void initView(View rootView) {
        LoadingDailog.Builder builder = new LoadingDailog.Builder(getActivity())
                .setMessage("加载中...")
                .setCancelable(true);
        loadingDailog = builder.create();
        gson = new Gson();

        rootView.findViewById(R.id.include_back).setVisibility(View.INVISIBLE);
        TextView title = rootView.findViewById(R.id.include_title);
        title.setText("购物车");
        TextView rightText = rootView.findViewById(R.id.include_right_text);
        rightText.setVisibility(View.VISIBLE);
        rightText.setOnClickListener(this);
        recycleview = rootView.findViewById(R.id.shopcart_recycler);
        /*设置属性*/
        recycleview.setLoadMore(false);//设置为可上拉加载,默认 false,调用这个方法 false 可以去掉底部的“加载更多”
        recycleview.setRefresh(true);// 设置为可下拉刷新,默认 true
        recycleview.setAutoLoadMore(false);//设置滑动到底部自动加载,默认 false
        recycleview.setOnItemClickListener(this);// 条目点击,点击和长按监听
        recycleview.setLFRecyclerViewListener(this);//下拉刷新上拉加载监听

//        recycleview.setScrollChangeListener(this);//滑动监听
//        recycleview.hideTimeView();//隐藏时间,默认显示时间
//        recycleview.setHeaderView(tv);//设置一个头部,只有一个大概满足了多数的要求
//        recycleview.setFootView(tv);//设置一个底部
//        recycleview.setNoDateShow();//没有数据时,底部显示"没有数据"字样,默认不显示

        /*添加适配器*/
        shoppingCartAdapter=new ShoppingCartAdapter(R.layout.shopping_cart_item,null);
        shoppingCartAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        recycleview.setAdapter(shoppingCartAdapter);
        shopCartList();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.include_right_text:

                break;
        }
    }

    @Override
    public void onRefresh() {
        shopCartList();
    }

    @Override
    public void onLoadMore() {

    }

    @Override
    public void onClick(int i) {

    }

    @Override
    public void onLongClick(int i) {

    }

    private void shopCartList() {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", SPUtils.getAppUserId(getActivity()));
        OkGo.<String>post(Constants.shopCartList)
                .params("user_id", SPUtils.getAppUserId(getActivity()))
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        setData(response);
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();
                        ShopCartListBean shopCartListBean = gson.fromJson(response.body(), ShopCartListBean.class);

                        if (shopCartListBean.getCode() >= 0) {
                            shoppingCartAdapter.setNewData(shopCartListBean.getResult());
                        } else {
                            Toast.makeText(getActivity(), shopCartListBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }

                    }

                });
    }
}
